package com.system.runtai.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.system.runtai.entity.ArticleCollect;
import com.system.runtai.entity.CompanyDynamic;
import com.system.runtai.entity.CompanyIntroduction;
import com.system.runtai.entity.SecondaryCopywriter;
import com.system.runtai.service.ArticleCollectService;
import com.system.runtai.service.CompanyDynamicService;
import com.system.common.result.Result;
import com.system.runtai.service.SecondaryCopywriterService;
import com.system.runtai.utils.AuthContextUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 公司动态 前端控制器
 * </p>
 *
 * @author runtai
 * @since 2024-03-29
 */
@Api(tags = "公司动态")
@RestController
@RequestMapping("/runtai/company-dynamic")
public class CompanyDynamicController {

    @Autowired
    private CompanyDynamicService companyDynamicService;

    @Autowired
    private SecondaryCopywriterService secondaryCopywriterService;

    @Autowired
    private ArticleCollectService articleCollectService;


    @ApiOperation(value = "公司动态二级")
    @GetMapping("/getSecondaryCopywriter/{id}")
    public Result<List<SecondaryCopywriter>> getSecondaryCopywriter(@PathVariable Integer id,@RequestParam(required = false) String title){
        if (StringUtils.isEmpty(title)) {
            LambdaQueryWrapper<SecondaryCopywriter> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(SecondaryCopywriter::getStartid, 3).eq(SecondaryCopywriter::getMainArgumentId, id);
            List<SecondaryCopywriter> list = secondaryCopywriterService.list(wrapper);
            return Result.ok(list);
        }else if (!StringUtils.isEmpty(title)){
            LambdaQueryWrapper<SecondaryCopywriter> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(SecondaryCopywriter::getStartid, 3).eq(SecondaryCopywriter::getMainArgumentId, id).like(SecondaryCopywriter::getTitle,title);
            List<SecondaryCopywriter> list = secondaryCopywriterService.list(wrapper);
            return Result.ok(list);
        }

        List<SecondaryCopywriter> list = null;
        return Result.fail(list).message("对象为空");
    }


    @ApiOperation(value = "添加公司动态二级文案")
    @PostMapping("/saveSecondaryCopywriter/{id}")
    public Result<SecondaryCopywriter> saveSecondaryCopywriter(@PathVariable Integer id,@RequestBody SecondaryCopywriter secondaryCopywriter){

        secondaryCopywriter.setCreateTime(LocalDateTime.now());
        secondaryCopywriter.setStartid(3);
        secondaryCopywriter.setMainArgumentId(id);
        boolean save = secondaryCopywriterService.save(secondaryCopywriter);

        if (save){
            return Result.ok(secondaryCopywriter).message("添加成功");
        }
        return Result.fail(secondaryCopywriter).message("添加失败");
    }


    @ApiOperation(value = "修改二级文案")
    @PostMapping("/updateSecondaryCopywriter")
    public Result<SecondaryCopywriter> updateSecondaryCopywriter(@RequestBody SecondaryCopywriter secondaryCopywriter){

        secondaryCopywriter.setUpdateTime(LocalDateTime.now());

        boolean b = secondaryCopywriterService.updateById(secondaryCopywriter);

        if (b){
            return Result.ok(secondaryCopywriter).message("修改成功");
        }
        return Result.fail(secondaryCopywriter).message("修改失败");
    }

    @ApiOperation(value = "删除二级文案")
    @PostMapping("delectSecondaryCopywriter/{id}")
    public Result delectSecondaryCopywriter(@PathVariable Integer id){

        boolean b = secondaryCopywriterService.removeById(id);

        if (b){
            return Result.ok().message("删除成功");
        }
        return Result.fail().message("删除失败");
    }


    @ApiOperation(value = "所有公司动态")
    @GetMapping("/companyDynamicList")
    public Result<List<CompanyDynamic>> companyDynamicList(){
        List<CompanyDynamic> list = companyDynamicService.list();
        return Result.ok(list);
    }

    @ApiOperation(value = "后台公司动态详情")
    @GetMapping("/companyDynamicht/{id}")
    public Result<CompanyDynamic> companyDynamicByidht(@PathVariable Integer id){
        CompanyDynamic byId = companyDynamicService.getById(id);
            return Result.ok(byId);
    }

    @ApiOperation(value = "程序公司动态详情")
    @GetMapping("/companyDynamiccx/{id}")
    public Result<CompanyDynamic> companyDynamicByidcx(@PathVariable Integer id){

        String s = AuthContextUtil.get();
        CompanyDynamic byId = companyDynamicService.getById(id);

        LambdaQueryWrapper<ArticleCollect> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ArticleCollect::getAppletUserId,Integer.valueOf(s)).eq(ArticleCollect::getStartId, 3).eq(ArticleCollect::getInRegardOfId, id);
        ArticleCollect one = articleCollectService.getOne(wrapper);

        if (one == null){
            byId.setCollect(0);
            return Result.ok(byId);
        }else {
            byId.setCollect(1);
            return Result.ok(byId);
        }
    }

    @ApiOperation(value = "查看所有公司动态一、二级文案")
    @GetMapping("/getCompanyDynamicSC/{startId}")
    public Result<IPage<CompanyDynamic>> getCompanyDynamicList(@PathVariable Integer startId,@RequestParam(required = false) Long page,@RequestParam(required = false) Long limit){
        List<CompanyDynamic> list = companyDynamicService.list();
//        for (int i = 0; i < list.size(); i++) {
//            LambdaQueryWrapper<SecondaryCopywriter> wrapper = new LambdaQueryWrapper<>();
//            wrapper.eq(SecondaryCopywriter::getStartid,startId).eq(SecondaryCopywriter::getMainArgumentId,list.get(i).getId());
//            List<SecondaryCopywriter> copywriterList = secondaryCopywriterService.list(wrapper);
//            list.get(i).setSecondaryCopywriterList(copywriterList);
//        }

        Page<CompanyDynamic> recommendPage = new Page<>(page,limit);

        LambdaQueryWrapper<CompanyDynamic> wrapper = new LambdaQueryWrapper<>();

        Integer id = null;
        for (CompanyDynamic companyDynamic : list) {
           id = companyDynamic.getId();
        }
        if (id!=null){
            wrapper.eq(CompanyDynamic::getId,id).orderByDesc(CompanyDynamic::getCreateTime);
        }

        IPage<CompanyDynamic> iPage = companyDynamicService.page(recommendPage,wrapper);
        for (CompanyDynamic record : iPage.getRecords()) {
            LambdaQueryWrapper<SecondaryCopywriter> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(SecondaryCopywriter::getStartid,startId).eq(SecondaryCopywriter::getMainArgumentId,record.getId());
            List<SecondaryCopywriter> copywriterList = secondaryCopywriterService.list(wrapper1);
            record.setSecondaryCopywriterList(copywriterList);
        }
        return Result.ok(iPage);
    }

    @ApiOperation(value = "添加公司动态")
    @PostMapping("/saveCompanyDynamic")
    public Result<CompanyDynamic> saveCompanyDynamic(@RequestBody CompanyDynamic companyDynamic){

        companyDynamic.setCreateTime(LocalDateTime.now());
        boolean save = companyDynamicService.save(companyDynamic);

        if (save){
            return Result.ok(companyDynamic).message("添加成功");
        }else {
            return Result.ok(companyDynamic).message("添加失败");
        }
    }


    @ApiOperation(value = "修改公司动态")
    @PostMapping("/updateCompanyDynamic")
    public Result<CompanyDynamic> updateCompanyDynamic(@RequestBody CompanyDynamic companyDynamic){

        companyDynamic.setUpdateTime(LocalDateTime.now());
        boolean update = companyDynamicService.updateById(companyDynamic);

        if (update){
            return Result.ok(companyDynamic).message("修改成功");
        }else {
            return Result.ok(companyDynamic).message("修改失败");
        }
    }


    @ApiOperation(value = "删除公司动态")
    @PostMapping("delectCompanyDynamic/{id}")
    public Result delectCompanyDynamic(@PathVariable Integer id){

        boolean b = companyDynamicService.removeById(id);

        if (b){
            return Result.ok().message("删除成功");
        }
        return Result.fail().message("删除失败");
    }

    @ApiOperation(value = "分页查询公司动态")
    @GetMapping("/{page}/{limit}")
    public Result<IPage<CompanyDynamic>> index12(@PathVariable Long page, @PathVariable Long limit, CompanyDynamic companyDynamic){

        Page<CompanyDynamic> recommendPage = new Page<>(page,limit);

        LambdaQueryWrapper<CompanyDynamic> wrapper = new LambdaQueryWrapper<>();

        Integer id = companyDynamic.getId();

        if (id!=null){
            wrapper.eq(CompanyDynamic::getId,id);
        }

        IPage<CompanyDynamic> iPage = companyDynamicService.page(recommendPage,wrapper);

        return Result.ok(iPage);
    }

    @ApiOperation(value = "收藏公司动态：3")
    @PostMapping("collectCompanyDynamic")
    public Result collectCompanyDynamic(@RequestBody CompanyDynamic companyDynamic) {

        String s = AuthContextUtil.get();

        LambdaQueryWrapper<ArticleCollect> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ArticleCollect::getInRegardOfId, companyDynamic.getId()).eq(ArticleCollect::getAppletUserId, Integer.valueOf(s)).eq(ArticleCollect::getStartId,3);
        ArticleCollect one = articleCollectService.getOne(wrapper);

        if (one!=null){
            return Result.ok(one);
        }else {
            ArticleCollect articleCollect = new ArticleCollect();
            articleCollect.setCreateTime(LocalDateTime.now());
            articleCollect.setAppletUserId(Integer.valueOf(s));
            articleCollect.setInRegardOfId(companyDynamic.getId());
            articleCollect.setStartId(3);
            boolean b = articleCollectService.save(articleCollect);
            if (b) {
                return Result.ok().message("添加收藏成功");
            }
            return Result.fail().message("添加收藏失败");
        }
    }




    @ApiOperation(value = "删除公司动态：3收藏")
    @PostMapping("delectCompanyDynamicCollect")
    public Result delectCompanyDynamicCollect(@RequestBody CompanyDynamic companyDynamic) {
        String s = AuthContextUtil.get();

        LambdaQueryWrapper<ArticleCollect> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ArticleCollect::getInRegardOfId, companyDynamic.getId()).eq(ArticleCollect::getAppletUserId, Integer.valueOf(s)).eq(ArticleCollect::getStartId,3);
        boolean b = articleCollectService.removeById(wrapper);

        if (b) {
            return Result.ok().message("删除成功");
        }
        return Result.fail().message("删除失败");
    }

}

